#'@title Plot cell sinking data with segmented regression fits
#'@description This function plots square root fluorescence versus elapsed time with fitted segmented regression lines for each well on a well plate.
#'@usage plot_fits(database_rfu, database_fits, size = 3, shape = 6, colour = "darkgreen",
#'fill = "white", shared_int = FALSE, line_1 = "red", line_2 = "blue", line_3 = "black",
#'export_output = FALSE, save_as = "Sinking_Plots.pdf")
#'@param database_rfu dataframe containing the columns "Plate", "Well", "Elapsed.Time.m", and "RFU". Can be generated by the user, or by running the import functions on raw data files.
#'@param database_fits dataframe containing the segmented regression parameters that will be used to calculate sinking rate. Generated by running sink_fit().
#'@param size point size.
#'@param shape point shape.
#'@param colour point outline colour.
#'@param fill point fill colour.
#'@param shared_int whether lines for each segment should be plotted with their respective y-intercepts (the default), or if the intercept of the first segment should be used for all lines.
#'@param line_1 line colour of first segment.
#'@param line_2 line colour of second segment.
#'@param line_3 line colour of third segment.
#'@param export_output whether the resulting dataframe should be exported to .csv.
#'@param save_as desired filename, if results are to be exported.
#'@return Plots of square root fluorescence (sqrt(RFU)) versus elapsed time for each well on a well plate, with segmented regression lines. Either exported to .pdf or appear in the plot viewer.
#'@examples
#'plot_fits(database_rfu, database_fits, export_output = TRUE)
# --------------------------------------------------------------------------------------------
plot_fits <- function(database_rfu, database_fits, size = 3, shape = 6, colour = "darkgreen", fill = "white",
shared_int = FALSE, line_1 = "red", line_2 = "blue", line_3 = "black",
export_output = FALSE, save_as = "Sinking_Plots.pdf") {
# -------------------------------------------------------------------------------------------
database <- dplyr::inner_join(database_rfu, database_fits)
plot_by <- paste(database$Plate, database$Well)
wells <- unique(plot_by)
if (export_output == TRUE) {
pdf(save_as)
}
for (w in 1:length(wells)) {
focus_well <- wells[w]
well_data <- dplyr::filter(database, focus_well == plot_by)
p <- ggplot2::ggplot(well_data, ggplot2::aes(Elapsed.Time.m, sqrt(RFU))) +
ggplot2::geom_point(size = size, shape = shape, colour = colour, fill = fill) +
ggplot2::theme_classic() +
ggplot2::labs(x = "Elapsed time (minutes)", y = "sqrt(RFU)", title = well_data$Well) +
ggplot2::theme(axis.title = ggplot2::element_text(size = 16, colour = "black", face = "bold")) +
ggplot2::theme(axis.text = ggplot2::element_text(size = 16, colour = "black")) +
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5, size = 18, face = "bold"))
if (shared_int == FALSE) {
p <- p +
ggplot2::geom_abline(intercept = well_data$intercept_1, slope = well_data$slope_1, colour = line_1, lty = 2, size = 1.25) +
ggplot2::geom_abline(intercept = well_data$intercept_2, slope = well_data$slope_2, colour = line_2, lty = 2, size = 1.25) +
ggplot2::geom_abline(intercept = well_data$intercept_3, slope = well_data$slope_3, colour = line_3, lty = 2, size = 1.25)
} else if (shared_int == TRUE) {
p <- p +
ggplot2::geom_abline(intercept = well_data$intercept_1[1], slope = well_data$slope_1, colour = line_1, lty = 2, size = 1.25) +
ggplot2::geom_abline(intercept = well_data$intercept_1[1], slope = well_data$slope_2, colour = line_2, lty = 2, size = 1.25) +
ggplot2::geom_abline(intercept = well_data$intercept_1[1], slope = well_data$slope_3, colour = line_3, lty = 2, size = 1.25)
}
print(p)
} # close loop through wells
if (export_output == TRUE) {
dev.off()
} # close device
} # end of function
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.